package com.fresh.bus.chat.business;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.fresh.bus.canoe.session.SessionRoleManager;
import com.fresh.bus.chat.domain.BaseLog;
import com.fresh.bus.chat.domain.TreeLog;
import com.fresh.bus.chat.domain.UserLog;
import com.fresh.bus.chat.domain.Water;
import com.fresh.bus.chat.util.DateUtil;

@Component
public class TreeLogService extends BaseLogService {

	private StringRedisTemplate template;
	@Resource
	protected SessionRoleManager roleManager;
	@Resource
	protected WealthService wealthService;
	@Resource
	private UserService userService;

	@Autowired
	public TreeLogService(StringRedisTemplate template) {
		this.template = template;
	}

	public void updateLog(String owner, String from, int actionType, String msgKey) {
		String tid = KeyUtils.tid(owner);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(tid);
		String treeLog = ops.get("treeLog");
		treeLog = appendLog(treeLog, from, actionType, msgKey);
		ops.put("treeLog", treeLog);
	}
	
	protected String appendLog(String srcLog, String who,int actionType, String msgKey) {
		TreeLog log = new TreeLog();
		String now = DateUtil.convertDateToString(new Date());
		log.setD(now);
		log.setU(who);
		log.setA(actionType);
		log.setT(msgKey);
		srcLog = appendLog(srcLog, log);
		return srcLog;
	}	

	// 取得贡献值最大的前三名
	public List<String> getDonors(String treeLog) {
		List<String> result = new ArrayList<String>();
		try {
			Map<String, Integer> map = new HashMap<String, Integer>();
			JsonFactory f = objectMapper.getJsonFactory();
			JsonParser jp = f.createJsonParser(treeLog);
			// advance stream to START_ARRAY first:
			jp.nextToken();
			int v;
			String user;
			Integer oldValue;
			// and then each time, advance to opening START_OBJECT
			while (jp.nextToken() == JsonToken.START_OBJECT) {
				BaseLog log = objectMapper.readValue(jp, BaseLog.class);
				if (log.getA() == TreeLog.SONGSHUI) {
					user = log.getU();
					v = Water.findPlantValue(Integer.valueOf(log.getT()));
					oldValue = map.get(user);
					if (oldValue != null) {
						v += oldValue;
					}
					map.put(user, v);
				}
			}

			List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(
					map.entrySet());

			Collections.sort(list,
					new Comparator<Map.Entry<String, Integer>>() {
						public int compare(Map.Entry<String, Integer> o1,
								Map.Entry<String, Integer> o2) {
							return (o2.getValue() - o1.getValue());
						}
					});

			String it;
			Map.Entry<String, Integer> e;
			int len = Math.min(list.size(), 3);
			for (int i = 0; i < len; i++) {
				e = list.get(i);
				it = e.getKey() + "," + e.getValue();
				result.add(it);
			}
		} catch (Exception ex) {
			log.error("", ex);
		}
		return result;
	}
}
